/****** Object:  Stored Procedure [dbo].spDongMoKyInsert Script Date: 8/7/2012 ******/
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[spDongMoKyInsert]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
	DROP PROCEDURE [dbo].[spDongMoKyInsert]
GO

/******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:					Description:
**		--------	--------				-----------------------------------
**		13/08/2012	SongTM					Innit created
*******************************************************************************/

CREATE PROCEDURE [dbo].[spDongMoKyInsert]
(
	@MaKy nvarchar(8)output,
	@NgayMoKy datetime,
	@NgayDongKy datetime
)
WITH ENCRYPTION

AS

SET NOCOUNT ON
Set @MaKy=RIGHT(year(@NgayMoKy), 4) + '-' + RIGHT('0' + CAST(month(@NgayMoKy) as varchar(2)), 2)


INSERT INTO [tbl_DongMoKy]
(
	[MaKy],
	[NgayMoKy],
	[NgayDongKy])
VALUES
(
	@MaKy,
	@NgayMoKy,
	@NgayDongKy
	
)
--insert new record to Dongmokychitiet
INSERT INTO tbl_chitietdongmoky (MaKy, MaLuuKho, SoDauKy)
SELECT @MaKy, tbl_luukho.MaluuKho,tbl_luukho.TonKho
FROM tbl_luukho
GO

--GRANT EXECUTE ON [dbo].[spDongMoKyInsert] TO [sa]
--GO